package com.yt.sys.server.api.cms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yt.common.constant.Result;
import com.yt.sys.models.dto.SysRolePermDTO;
import com.yt.sys.models.entity.SysRolePerm;
import com.yt.sys.models.service.ISysRolePermService;
import com.yt.sys.models.service.ISysRoleService;
import com.yt.sys.models.service.ISysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/cms/sys/role-perm")
@Api(tags = "系统管理-角色权限关联管理")
public class SysRolePermController {

    @Autowired
    private ISysRolePermService sysRolePermService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysPermissionService sysPermissionService;

    @GetMapping("/list")
    @ApiOperation("角色权限关联列表")
    public Result<List<SysRolePerm>> list() {
        QueryWrapper<SysRolePerm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<SysRolePerm> list = sysRolePermService.list(queryWrapper);
        return Result.success(list);
    }

    @GetMapping("/role/{roleId}")
    @ApiOperation("根据角色ID获取权限列表")
    public Result<List<SysRolePerm>> getByRoleId(@PathVariable Integer roleId) {
        // 检查角色是否存在
        if (sysRoleService.getById(roleId) == null) {
            return Result.fail("角色不存在");
        }

        QueryWrapper<SysRolePerm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        queryWrapper.eq("is_delete", 0);
        List<SysRolePerm> list = sysRolePermService.list(queryWrapper);
        return Result.success(list);
    }

    @PostMapping
    @ApiOperation("新增角色权限关联")
    public Result<String> add(@Valid @RequestBody SysRolePermDTO sysRolePermDTO) {
        // 检查角色是否存在
        if (sysRoleService.getById(sysRolePermDTO.getRoleId()) == null) {
            return Result.fail("角色不存在");
        }

        // 检查权限是否存在
        if (sysPermissionService.getById(sysRolePermDTO.getPermId()) == null) {
            return Result.fail("权限不存在");
        }

        // 检查是否已存在关联关系
        QueryWrapper<SysRolePerm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", sysRolePermDTO.getRoleId());
        queryWrapper.eq("perm_id", sysRolePermDTO.getPermId());
        queryWrapper.eq("is_delete", 0);
        if (sysRolePermService.count(queryWrapper) > 0) {
            return Result.fail("角色权限关联关系已存在");
        }

        SysRolePerm rolePerm = new SysRolePerm();
        BeanUtils.copyProperties(sysRolePermDTO, rolePerm);
        rolePerm.setIsDelete(0);
        boolean result = sysRolePermService.save(rolePerm);
        if (result) {
            return Result.success("关联成功");
        } else {
            return Result.fail("关联失败");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除角色权限关联")
    public Result<String> delete(@PathVariable Integer id) {
        SysRolePerm rolePerm = sysRolePermService.getById(id);
        if (rolePerm == null || rolePerm.getIsDelete() == 1) {
            return Result.fail("角色权限关联关系不存在");
        }

        // 逻辑删除
        rolePerm.setIsDelete(1);
        boolean result = sysRolePermService.updateById(rolePerm);
        if (result) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }

    @DeleteMapping("/role/{roleId}/perm/{permId}")
    @ApiOperation("根据角色ID和权限ID删除关联关系")
    public Result<String> deleteByRoleIdAndPermId(@PathVariable Integer roleId, @PathVariable Integer permId) {
        QueryWrapper<SysRolePerm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        queryWrapper.eq("perm_id", permId);
        queryWrapper.eq("is_delete", 0);
        
        SysRolePerm rolePerm = sysRolePermService.getOne(queryWrapper);
        if (rolePerm == null) {
            return Result.fail("删除失败，关联关系不存在");
        }

        // 逻辑删除
        rolePerm.setIsDelete(1);
        boolean result = sysRolePermService.updateById(rolePerm);
        if (result) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }
}